// miniprogram/group_admin/pages/faileSave/faileSave.js
// 获取api
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

   // 显示错题
  faileNo:function(){
    this.showHideToast();
    var contentList=this.data.wrongContentList;
    this.setData({
      isTrue: 0,
      "contentList":contentList
    })
  },
   // 显示收藏
  saveYes:function(){
    this.showHideToast();
    var contentList=this.data.collectContentList;
    this.setData({
      isTrue: 1,
      "contentList":contentList
    })
 },
    // 显示 隐藏Toast
    showHideToast:function(){
      wx.showLoading({
        title: '加载中',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
    },

      //单击错题/收藏 item 7- 错题 12- 收藏
  clickItem:function(e){
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var courseInfo= wx.getStorageSync("COURSEINFO");
    // var moduleList= wx.getStorageSync("MODULELIST");
    var type=7;
    if(this.data.isTrue==1){
      type=12;
    }
    var unitid=e.currentTarget.dataset.unitid;
    const parmas={
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "courseid": courseInfo.id,
      "market": getApp().globalData.market,
      // "type": moduleList.module,
      "type": type,
      "unitid":unitid,
      from:getApp().globalData.from
    }
    //页面传参跳转
    wx.navigateTo({
    url: '../tiku/tiku?parmas='+JSON.stringify(parmas)
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取首页传递过来的错题0 收藏1  
    var isTrue=options.isTrue;
    var that=this;
    if(isTrue==""||isTrue==null){
      that.setData({
        "isTrue":0
      }) 
    }else{
      that.setData({
        "isTrue":isTrue
      }) 
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.showHideToast();
    this.getWrongList();
    this.getCollectList();
  },

  // 获取错题列表
  getWrongList:function(){
     // 获取用户的sessionid uid 课程id
     var resYtkUserInfo= wx.getStorageSync("USERDATA");
     var courseInfo= wx.getStorageSync("COURSEINFO");
     var categoryInfo= wx.getStorageSync("CATEGORYINFO");
    //参数
     var params = {
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "market": "weixinapp_ytku",
      "categoryId":categoryInfo.id,
      "courseIds":courseInfo.id,
    };
    //url地址
    var url = "/myStudyCenter/findWrongQuestionList";
    var that = this;
    api.sendPosts( url,params, function(res) {
      that.setData({
        "wrongList": res.wrongList
        });

      //遍历错题列表数组
      for (var i in that.data.wrongList) {
        var courseId = that.data.wrongList[i].courseId;
      }
      that.data.courseId = courseId;
      that.getWrongContent();
    });
  },
  //获取收藏列表
  getCollectList:function(){
    // 获取用户的sessionid uid 课程id
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var courseInfo= wx.getStorageSync("COURSEINFO");
    var categoryInfo= wx.getStorageSync("CATEGORYINFO");
   //参数
    var params = {
     "sessionid": resYtkUserInfo.sessionid,
     "uid": resYtkUserInfo.uid,
     "market": "weixinapp_ytku",
     "categoryId":categoryInfo.id,
     "courseIds":courseInfo.id,
   };
   //url地址
   var url = "/myStudyCenter/findCollectList";
   var that = this;
   api.sendPosts( url,params, function(res) {
     that.setData({
       "collectList": res.collectList
       });
       //遍历错题列表数组
      for (var i in that.data.collectList) {
        var courseId = that.data.collectList[i].courseId;
      }
      that.data.courseId = courseId;
      that.getCollectContent();
   });
 },

  //获取错题章节列表
  getWrongContent:function(){
     // 获取用户的sessionid uid 课程id
     var resYtkUserInfo= wx.getStorageSync("USERDATA");
    //参数
     var params = {
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "courseId":this.data.courseId,
    };
    //url地址
    var url = "/myStudyCenter/findWrongUnitList";
    var that = this;
    api.sendPosts( url,params, function(res) {
      that.setData({
        "wrongContentList": res.list,
        });
        if(that.data.isTrue==0){
          that.setData({
            "contentList":res.list
          })
        }
        
    });
  },
  //获取收藏章节列表
  getCollectContent:function(){
    // 获取用户的sessionid uid 课程id
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    //参数
     var params = {
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "courseId":this.data.courseId,
      "market": getApp().globalData.market
    };
    //url地址
    var url = "/myStudyCenter/findCollectUnitList";
    var that = this;
    api.sendPosts( url,params, function(res) {
      that.setData({
        "collectContentList": res.list
        });
        if(that.data.isTrue==1){
          that.setData({
            "contentList":res.list
          })
        }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})